package com.sohoometer.station.action;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.nari.basic.model.IUser;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResultWithPageCond;
import com.sohoometer.base.action.BaseAction;
import com.sohoometer.station.bean.WaterMeterData;
import com.sohoometer.station.bean.WaterMeterInfo;
import com.sohoometer.station.services.WaterMeterDataService;
import com.sohoometer.station.services.WaterMeterInfoService;

@Controller
@Scope("prototype")
public class WaterDataAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private String waterMeterNo ;
	private Date startQueryTime;
	private Date endQueryTime;
	//根据电表表号所对应的电表信息
	private HashMap<String, WaterMeterInfo> meterMap;
	@Autowired
	private  WaterMeterInfoService waterMeterInfoService = null;
	@Autowired
	private WaterMeterDataService waterMeterDataService = null;
	private ResultWithPageCond<WaterMeterData> datas = null;
	/**
	  * 查询电表相关信息以及电表数据
	  * 根据电表表号
	 * @return 	 beshs/powerMeter/powerMeterDataList.jsp
	 * @throws Exception 
	 */
	
	public String queryList() throws Exception {
		//查询当前登录用户
		IUser user = getUser();
	List<WaterMeterInfo>	 meters = waterMeterInfoService.queryWaterInfoByUserId(user.getId());
		//查询当前登录用户所授权的电表信息 并存入HashMap
		 this.meterMap = list2map(meters);
		pageCond =new PageCond(); //处理分页
		int start = (this.pageIndex - 1) * this.pageCond.getLimit();
		this.pageCond.setStart(start);
		//根据水表表号进行查询
		datas=waterMeterDataService.queryWaterMeterDatas(waterMeterNo,meters,startQueryTime,endQueryTime,pageCond);
		this.pageSize = new BigDecimal(datas.getPageCond().getTotal()).divide(new BigDecimal(datas.getPageCond().getLimit()), RoundingMode.UP).intValue();
		if(pageSize == 0) pageSize = 1;
		return "list";
	}
	
	private HashMap<String, WaterMeterInfo> list2map(
List<WaterMeterInfo> meters) {
	HashMap<String, WaterMeterInfo>	meterMap = new HashMap<String, WaterMeterInfo>();
	if(meters==null) return null;
		for(WaterMeterInfo info:meters) {
			if(StringUtils.isBlank(info.getWaterMeterNo())) continue;
			meterMap.put(info.getWaterMeterNo(), info);
	}
		return meterMap;
	}

	public String getWaterMeterNo() {
		return waterMeterNo;
	}

	public void setWaterMeterNo(String waterMeterNo) {
		this.waterMeterNo = waterMeterNo;
	}

	public Date getStartQueryTime() {
		return startQueryTime;
	}
	public void setStartQueryTime(Date startQueryTime) {
		this.startQueryTime = startQueryTime;
	}
	public Date getEndQueryTime() {
		return endQueryTime;
	}
	public void setEndQueryTime(Date endQueryTime) {
		this.endQueryTime = endQueryTime;
	}
	public HashMap<String, WaterMeterInfo> getMeterMap() {
		return meterMap;
	}
	public void setMeterMap(HashMap<String, WaterMeterInfo> meterMap) {
		this.meterMap = meterMap;
	}

	public ResultWithPageCond<WaterMeterData> getDatas() {
		return datas;
	}

	public void setDatas(ResultWithPageCond<WaterMeterData> datas) {
		this.datas = datas;
	}
	
}
